package com.reliable.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.reliable.api.dto.user.ReliableUserLite;
import com.reliable.api.param.invite.InviteSaveParam;
import com.reliable.api.param.invite.InviteUpdateStatus;
import com.reliable.common.core.domain.AjaxResult;
import com.reliable.common.utils.StringUtils;
import com.reliable.common.utils.uuid.UUID;
import com.reliable.framework.utils.JwtUtils;
import com.reliable.service.domain.ReliableInvite;
import com.reliable.service.domain.ReliableUser;
import com.reliable.service.enums.InviteStatusEnum;
import com.reliable.service.enums.ReliableBannedType;
import com.reliable.service.enums.ReliableInviteUseStatus;
import com.reliable.service.mapper.ReliableInviteMapper;
import com.reliable.service.service.IReliableInviteService;
import com.reliable.service.service.IReliableUserService;
import com.reliable.service.utils.IdEncryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 邀请码Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-09
 */
@Service
public class ReliableInviteServiceImpl extends ServiceImpl<ReliableInviteMapper, ReliableInvite> implements IReliableInviteService {


    @Autowired
    private IReliableUserService userService;


    @Override
    public AjaxResult create(InviteSaveParam param) {

        ReliableUserLite currentUser = JwtUtils.getCurrentUser();

        if (currentUser.getUserType()!=1L)
        {
            return AjaxResult.warn("您没有权限");
        }


        //  判断是否设置验证码有效期
        if (StringUtils.isNull(param.getExpireTime()))
        {
            return AjaxResult.error("请设置验证码有效期");
        }

        // 生成验证码 uuid+26大小写 六位数
        String inviteCode = UUID.randomUUID().toString().toLowerCase();

        if (StringUtils.isNotEmpty(param.getInviteCodePrefix()))
        {
                // 拼接前缀
            inviteCode = param.getInviteCodePrefix() +"-"+ inviteCode;
        }else
        {
            inviteCode = "MC-"+inviteCode;
        }

        ReliableInvite xssInvite = new ReliableInvite();
        xssInvite.setInviteCode(inviteCode);
        xssInvite.setExpireTime(param.getExpireTime());
        xssInvite.setUseStatus(ReliableInviteUseStatus.UNUSED.getCode());
        // 设置新增的省市区和角色ID参数
        xssInvite.setProvince(param.getProvince());
        xssInvite.setCity(param.getCity());
        xssInvite.setDistrict(param.getDistrict());
        Long roleId = IdEncryptUtils.decrypt(param.getRoleId());
        xssInvite.setRoleId(Math.toIntExact(roleId));
        boolean save = this.save(xssInvite);
        if (save)
            return AjaxResult.success("生成邀请码成功");
        return AjaxResult.error("生成邀请码失败");
    }

    @Override
    public AjaxResult updateStatus(InviteUpdateStatus param) {

        ReliableInvite xssInvite = baseMapper.selectById(param.getId());
        if (xssInvite == null)
        {
            return AjaxResult.warn("邀请码不存在");
        }

        if (param.getStatus() == InviteStatusEnum.DISABLED.getCode()){

            LambdaUpdateWrapper<ReliableUser> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ReliableUser::getId, xssInvite.getUserId())
                    .set(ReliableUser::getBannedType, ReliableBannedType.BANNED.getCode());

            userService.update(updateWrapper);
        }
        xssInvite.setStatus(param.getStatus());
        boolean update = this.updateById(xssInvite);

        if (update)
            return AjaxResult.success("修改成功");
        return AjaxResult.warn("修改失败");
    }
}